#ifndef __VERTEX_ARRAY_HH__
#define __VERTEX_ARRAY_HH__

#include <glad/glad.h>

namespace opengl
{

//! @brief 顶点数组对象 VAO
class VertexArray
{
public:
    VertexArray() : _id(0)
    {
        glGenVertexArrays(1, &(this->_id));
    }

    void bind()
    {
        glBindVertexArray(this->_id);
    }

    void unbind()
    {
        glBindVertexArray(0);
    }

    void free()
    {
        glDeleteVertexArrays(1, &(this->_id));
        this->_id = 0;
    }

    void setVertexAttribPointer(GLuint index, GLint size, GLenum type, GLsizei stride, const void *pointer, bool enbale = true, GLboolean normalized = GL_FALSE)
    {
        glVertexAttribPointer(index, size, type, normalized, stride, pointer);
        if (enbale)
            glEnableVertexAttribArray(index);
    }

    void setVertexAttribDivisor(GLuint index, GLint divisor)
    {
        glVertexAttribDivisor(index, divisor);
    }

private:
    GLenum _id;
};

//! @brief 顶点缓冲对象 VBO
class VertexBuffer
{
public:
    VertexBuffer(GLenum type = GL_ARRAY_BUFFER) : 
        _id(0), _type(type)
    {
        glGenBuffers(1, &(this->_id));
    }

    void bind()
    {
        glBindBuffer(this->_type, this->_id);
    }

    void unbind()
    {
        glBindBuffer(this->_type, 0);
    }

    void free()
    {
        glDeleteBuffers(1, &(this->_id));
        this->_id = 0;
    }

    void setBufferData(GLsizeiptr size, const void *data, GLenum usage)
    {
        glBufferData(this->_type, size, data, usage);
    }

private:
    GLenum _id;
    GLenum _type;
};

}

#endif